package com.cn.jx.manager.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cn.jx.common.controller.BaseController;
import com.cn.jx.manager.po.DataGrid;
import com.cn.jx.manager.po.SystemConfig;
import com.cn.jx.manager.service.mtn.ISystemInfoServiceMtn;
import com.cn.jx.manager.service.query.IDataGridServiceQry;
import com.jx.enumType.DataGrideTypeEnum;
import com.jx.fileUtil.xml.Impl.SaxXml;
import com.jx.util.FormValidat;
import com.jx.util.MapObject;
import com.jx.util.Response;

/**
 * 
 * @author 姜旭
 * @version v1.11.20.2019
 * @since 
 * <p>创建时间：2019年12月2日
 * <p>功能描述：系统文件
 * <p>官方网站：management/admin/index
 * <p>邮箱地址：524577360@qq.com
 */
@Controller
@RequestMapping("/admin/system/")
public class SystemController extends BaseController{
	
	@Autowired
	private IDataGridServiceQry dataGridServiceQry;
	
	@Autowired
	private ISystemInfoServiceMtn systemInfoServiceMtn;

	/**
	 * 
	* @Title: dictlist
	* @Description: TODO( 跳转到系统设置页面)
	* <p>邮箱地址：524577360@qq.com
	* @throws
	 */
	@RequestMapping("base/setting")
	public String dictlist(Model model) {
		SystemConfig config= systemInfoServiceQry.findSystemConfig();
		model.addAttribute("res", config);
		
		MapObject mo=MapObject.getInstance();
		for (DataGrideTypeEnum sde : DataGrideTypeEnum.values()) {
			mo.put(sde.getId(), sde.getName());
		}
		model.addAttribute("mo", mo);
		return "system/setting";
	}
	
	/**
	 * 
	* @Title:userOrgJobList
	* @Description:TODO(动态列获取)
	* <p>邮箱地址: 524577360@qq.com
	* List<DataGrid>
	* @throws
	 */
	@RequestMapping("dynamic/column")
	@ResponseBody
	public List<DataGrid> userOrgJobList(Model model, String id) {
		try {
			List<DataGrid> pageuser = dataGridServiceQry.findAllGridByType(DataGrideTypeEnum.getEnumById(id));
			return pageuser;
		} catch (Exception e) {
			log.debug(e.getMessage());
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 
	* @Title: Widget
	* @Description: TODO(系统读取图标文件)
	* <p>邮箱地址：524577360@qq.com
	* @throws
	 */
	@RequestMapping("/widget/Icon")
	public String Widget(Model model, HttpServletRequest request) {
		// 图标选择器
		String hiddenId = request.getParameter("hiddenId");
		String imgId = request.getParameter("imgId");
		model.addAttribute("hiddenId", hiddenId);
		model.addAttribute("imgId", imgId);
		model.addAttribute("iconlist", SaxXml.getIcons());
		return "widget/IconWidget";
	}
	
	@RequestMapping("base/setting/do")
	@ResponseBody
	public Response setSystemcfg(Model model, HttpServletRequest request, String column,String value,String dtype) {
		// 表单验证
		try {
			FormValidat.validSetting(column, value, dtype);
			systemInfoServiceMtn.updataSystemConfig(column, value);
			return Response.Success();
		} catch (Exception e) {
			log.debug(e.getMessage());
			return Response.Fail(e.getMessage());
		}
	}
}
